<template>
  <el-dialog
    width="500px"
    :visible="showDialog"
    :title="bigTitle"
    @close="close"
  >
    <el-form ref="deptForm" :model="deptForm" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" size="mini" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门类型" prop="type">
        <el-select v-model="deptForm.type" size="mini">
          <el-option label="分公司" value="分公司" />
          <el-option label="部门" value="部门" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerName">
        <el-select v-model="deptForm.managerName" size="mini">
          <el-option label="张三三" value="张三三" />
          <el-option label="李四四" value="李四四" />
          <el-option label="何笑笑" value="何笑笑" />
          <el-option label="李维" value="李维" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门描述" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" style="width: 80%" :rows="2" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="16">
            <el-button size="mini" type="primary" @click="confirmClick">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartment, getDepartmentDetail, updateDepartment } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentPid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      deptForm: {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 8, message: '部门名称是1-8位字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '部门类型不能为空', trigger: 'blur' }
        ],
        managerName: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 弹层标题
    bigTitle() {
      return this.deptForm.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // 点击确定按钮
    confirmClick() {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          if (this.deptForm.id) {
            // 编辑模式
            await updateDepartment(this.deptForm)
            this.$message.success('修改部门成功')
          } else {
            // 新增模式
            await addDepartment({ ...this.deptForm, pid: this.currentPid })
            this.$message.success('新增部门成功')
          }
          this.$emit('addDepartmentSuccess')
          this.close()
        }
      })
    },
    async getDetail() {
      const res = await getDepartmentDetail(this.currentPid)
      console.log(res)
      this.deptForm = res.data
    },
    // 监听弹层关闭
    close() {
      // 重置表单数据对象
      this.deptForm = {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
